//
//  HYOUStrategyHeadView.m
//  BestGift
//
//  Created by 温晓宇 on 15/12/30.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "HYOUStrategyHeadView.h"
#import "HYOUStrategyHeadViewCell.h"
@interface HYOUStrategyHeadView ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic, retain)HYOUBaseScrollView *myScrollView;

@property(nonatomic, retain) HYOUBaseCollectionView *downCollectionView;


@property(nonatomic, retain) UILabel *title;

@end

@implementation HYOUStrategyHeadView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}

-(void)createSubViews{
    
    self.title = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 50, 50)];
    self.title.text = @"专题";
    [self addSubview:self.title];
    
    HYOUBaseButton *button = [HYOUBaseButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(WIDTH - 100, 5, 90, 50);
//    button.backgroundColor = [UIColor whiteColor];
    [button setTitle:@"查看更多" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self addSubview:button];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
    flowlayout.itemSize = CGSizeMake((WIDTH - 20) / 2, 80);
    flowlayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 10);
    flowlayout.minimumInteritemSpacing = 10;
    flowlayout.minimumLineSpacing = 10;
    flowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.downCollectionView = [[HYOUBaseCollectionView alloc]initWithFrame:CGRectMake(0, 50, WIDTH, 100) collectionViewLayout:flowlayout];
    //    self.downCollectionView.backgroundColor = [UIColor whiteColor];
    self.downCollectionView.delegate = self;
    self.downCollectionView.dataSource = self;
    [self addSubview:self.downCollectionView];
    
    [self.downCollectionView registerClass:[HYOUStrategyHeadViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    
//    self.myScrollView = [[HYOUBaseScrollView alloc] initWithFrame:CGRectMake(0, 50, WIDTH, 100)];
////    self.myScrollView.backgroundColor = [UIColor whiteColor];
//
//    [self addSubview:self.myScrollView];
//    
//    self.myScrollView.bounces = YES;
//    self.myScrollView.showsHorizontalScrollIndicator = NO;
//    self.myScrollView.showsVerticalScrollIndicator = NO;
    
//    for (int i = 0; i < 5; i++) {
//        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5 + 190 * i, 0, (WIDTH - 20) / 2, 80)];
//        imageView.backgroundColor = [UIColor cyanColor];
//        imageView.layer.cornerRadius = 5;
//        imageView.layer.masksToBounds = YES;
//        [self.myScrollView addSubview:imageView];
//    }
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.allArray.count;
}

//-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
//    return self.allArray.count;
//}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HYOUStrategyHeadViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (self.allArray.count != 0) {
        HYOUStrategyCellModel *model = [self.allArray objectAtIndex:indexPath.row];
        cell.model = model;
        
    }
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HYOUStrategyCellModel *model = [self.allArray objectAtIndex:indexPath.row];
    NSString *url =[NSString stringWithFormat:@"http://api.liwushuo.com/v2/collections/%@/posts?gender=1&generation=2&limit=20&offset=0",model.id];
    [self.delegate showTopic:url];
    NSLog(@"%@",url);
}

-(void)buttonAction:(UIButton *)btn{
    [self.delegate showSeeMore];
}

-(void)setAllArray:(NSMutableArray *)allArray{
    if (_allArray != allArray) {
        [_allArray release];
        _allArray = [allArray retain];
    }
//    NSLog(@"%ld",(unsigned long)self.allArray.count);
    [self.downCollectionView reloadData];
//        self.myScrollView.contentSize = CGSizeMake(5 + (WIDTH - 5) / 2 * self.allArray.count, 0);
////    NSLog(@"%ld", self.allArray.count);
//    for (int i = 0; i < self.allArray.count; i++) {
//        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5 + (WIDTH - 5) / 2 * i, 0, (WIDTH - 20) / 2, 80)];
////        imageView.backgroundColor = [UIColor cyanColor];
//        NSString *name = [self.allArray objectAtIndex:i];
//        NSURL *url = [NSURL URLWithString:name];
//        [imageView sd_setImageWithURL:url];
//        imageView.layer.cornerRadius = 5;
//        imageView.layer.masksToBounds = YES;
//        [self.myScrollView addSubview:imageView];
//    }
}

@end
